package GoodsService

import (
	"context"
	"fmt"
	"gdshop-font-api-go/app/request/GoodsReq"
	"gdshop-font-api-go/app/response/GoodsResp"
	"gdshop-font-api-go/library/response"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
	"strings"
)

func Content(ctx context.Context, parames *GoodsReq.Content) *response.JsonResponse {
	content := GetGoodsContent(ctx, parames.Id, parames.Type)
	photoPrefix := g.Cfg().GetString("site.PhotoUrlProfix")
	var mcTmps []*GoodsResp.MobileContentItem
	fmt.Println(content)
	err := gconv.Structs(content, &mcTmps)
	if err != nil {
		return response.SuccessByRequestMessageData(
			nil,
			"成功",
			[]string{},
		)
	}
	/*
		返回HTML的方式
		var htmls = []string{}
		// 处理图片
		for _, item := range mcTmps {
			if item.InputType == "textarea" {
				htmls = append(htmls, "<div class=\"textarea\">"+item.InputValue+"<div>")
			}
			if item.InputType == "photo" {
				for k, _ := range item.InputValues {
					if item.InputValues[k] != "" {
						htmls = append(htmls, "<img class=\"photo\" src=\""+photoPrefix+item.InputValues[k]+"\" />")
					}
				}
			}
		}

		return response.SuccessByRequestMessageData(
			nil,
			"成功",
			strings.Join(htmls, ""),
		)*/

	if mcTmps == nil {
		return response.SuccessByRequestMessageData(
			nil,
			"成功",
			[]string{},
		)
	}

	// 直接返回json结构的方式
	res := []*GoodsResp.MobileContentItemResp{}
	// 处理图片
	for _, item := range mcTmps {
		tmpOne := &GoodsResp.MobileContentItemResp{}
		tmpOne.InputType = item.InputType
		tmpOne.InputValue = item.InputValue
		tmpOne.Color = item.Color
		tmpOne.Bcolor = item.Bcolor
		tmpOne.InputValues = []string{}

		if item.InputType == "photo" {
			if item.InputValues != "" {
				tmps := strings.Split(item.InputValues, ",")
				for k, _ := range tmps {
					tmps[k] = photoPrefix + tmps[k]
				}
				tmpOne.InputValues = tmps
			}
		}
		res = append(res, tmpOne)
	}
	return response.SuccessByRequestMessageData(
		nil,
		"成功",
		res,
	)
}
